package com.example.myapplication.chap05;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.R;

//【例5-5】 创建与删除数据库
public class DBActivity extends AppCompatActivity implements View.OnClickListener {
    Button creatBtn, deleteBtn, readBtn;
    TextView txt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_05_db);
        txt = (TextView) findViewById(R.id.txt);
        creatBtn = (Button) findViewById(R.id.createdb);
        creatBtn.setOnClickListener(this);
        deleteBtn = (Button) findViewById(R.id.deletedb);
        deleteBtn.setOnClickListener(this);
        readBtn = (Button) findViewById(R.id.read_db);
        readBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        if (arg0 == creatBtn) {
            //
            SQLiteDatabase db = openOrCreateDatabase("zhou.db", MODE_PRIVATE, null);
            String sql = "create table IF NOT EXISTS zhou( nid integer primary key autoincrement ,name text not null);";

            db.execSQL(sql);
            ContentValues values = new ContentValues();
            values.put("name", "周老师");
            db.insert("zhou", "nid", values);
            db.close();
            Toast.makeText(DBActivity.this, "数据库和数据表创建成功", Toast.LENGTH_SHORT).show();
            txt.setText("创建数据库成功");
        } else if (arg0 == readBtn) {
            SQLiteDatabase db = openOrCreateDatabase("zhou.db", MODE_PRIVATE, null);
            String sql = "SELECT * FROM zhou;";
//            Cursor cursor = db.rawQuery(sql, null);
            Cursor cursor = db.query("zhou",new String[]{"nid","name"},null,null,null,null,null);
            int count = cursor.getCount();
            cursor.moveToFirst();
            for (int i = 0; i < count; i++) {
                int id = cursor.getInt(0);
                String name = cursor.getString(1);
                Toast.makeText(this, "序号=" + id + " 名字=" + name, Toast.LENGTH_LONG).show();
                cursor.moveToNext();
            }

//            cursor.moveToPrevious();
//            cursor.moveToLast();
//            cursor.moveToPosition(count);
//            cursor.move();
            Toast.makeText(this, "总数=" + count, Toast.LENGTH_LONG).show();
            db.close();
        } else if (arg0 == deleteBtn) {
            deleteDatabase("zhou.db");
            Toast.makeText(DBActivity.this, "数据库删除成功", Toast.LENGTH_SHORT).show();
            txt.setText("删除数据库成功");
        }
    }

    class MySQLDatabase {
        static final String Database_name = "eBook.db";

        private MySQLDatabase() {
            SQLiteDatabase db;
            String TABLE_NAME = "diary";            //数据表名
            String ID = "nid";                    //ID编号
            String TITLE = "title";                //用户名
            String BODY = "body";                //联系电话
            String DATABASE_CREATE =
                    "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
                            + ID + " INTEGER primary key autoincrement,"
                            + TITLE + " text not null, "
                            + BODY + " text not null);";
            Log.w("SQL", DATABASE_CREATE);
            int mode = Context.MODE_PRIVATE;
            db = openOrCreateDatabase(Database_name, mode, null);//创建数据库
            db.execSQL(DATABASE_CREATE);//创建数据表
            ContentValues values = new ContentValues();
            //String ssql = "insert into diary(nid,title,body) values( null, 'Andrid','物联网')";
            values.put(TITLE, "Android");
            values.put(BODY, "物联网");
            db.insert(TABLE_NAME, ID, values);
        }
    }
}

